package poidemo.controller;

import poidemo.commonutil.PoiExcelExport;
import poidemo.commonutil.ServletUtil;
import poidemo.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: peisong.chen
 * @Date: 2020/1/7 17:26
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {
    @GetMapping("/hello")
    public Object sayHello() {
        log.info("hello ----》 I'm the best!");
        return "hello Brothers! Ha ha ha ... ";
    }
    @GetMapping("/export")
    public void exportDemo(HttpServletRequest req, HttpServletResponse resp) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("小松", 398D));
        studentList.add(new Student("浩浩", 430D));
        studentList.add(new Student("博哥", 570D));
        // 执行导出
        String fileName = "Student.xls";
        ServletUtil su = new ServletUtil(fileName, req, resp);
        su.poiExcelServlet();

        String[] heads = {"Name", "Grade"};
        String[] cols = {"name", "grade"};
        // 这里传第几个字段是数字，从0开始
        int[] numerics = {1};
        ServletUtil suresp = new ServletUtil(resp);
        PoiExcelExport<Student> pee = new PoiExcelExport<>(fileName, heads, cols, studentList, numerics, suresp.getOut());
        pee.exportExcel();
    }
}
